package de.uni_postdam.ling.tcl.smc_lda;

import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.Properties;
import de.uni_postdam.ling.tcl.smc_lda.SequentialSampler.SamplerGenerator;

/**
 *
 */
public class App 
{
	/**
	 * 
	 */
	private final static String INITIAL_STEP_NUMBER = "initialSteps";
	
	/**
	 * 
	 */
	private final static String INITIAL_SAMPLE_SIZE = "initialSize";
	
	/**
	 * 
	 */
	private final static String BUFFERSIZE_PARAMETER = "sizeBuffer";
	
	/**
	 * 
	 */
	private final static String PARTICLES_PARAMETER = "nParticles";
	
	/**
	 * 
	 */
	private final static String THREADS_PARAMETER = "nThreads";
	
	/**
	 * 
	 * 
	 */
	private final static String SEED_PARAMETER = "seed";
	
	/**
	 * 
	 */
	private final static String HISTORY_SIZE_PARAMETER = "histSize";
	
	/**
	 * 
	 */
	private final static String REJUVENATION_STEPS_PARAMETER = "rejStep";
	
	/**
	 * 
	 */
	private final static String TOPIC_WORD_SMOOTHING = "tWSmoothing";
	
	/**
	 * 
	 */
	private final static String NUMBER_OF_TOPICS = "nTopics";
	
	/**
	 * 
	 */
	private final static String TOPIC_SMOOTHING = "tSmoothing";
	
	/**
	 * 
	 * @param args
	 * @throws IOException
	 * @throws InterruptedException 
	 */
    public static void main( String[] args ) throws IOException, InterruptedException
    {
    	Properties prop = new Properties();
    	
    	try(Reader r = new FileReader(args[0]))
    	{
    		prop.load(r);
    	}
    	
    	SamplerGenerator sg = SequentialSampler.generator();
    	
    	setUpGenerator(prop, sg);
    	
    	int previewSize = Integer.parseInt(prop.getProperty(BUFFERSIZE_PARAMETER, "100"));
    	WordLineDocumentSource source = new WordLineDocumentSource(new FileReader(args[1]), previewSize);
    	
    	int numberOfThreads = Math.max(1, Integer.parseInt(prop.getProperty(THREADS_PARAMETER, "1")));
    	int initialSize = Math.max(1, Integer.parseInt(prop.getProperty(INITIAL_SAMPLE_SIZE,"50")));
    	int initialSteps = Math.max(1, Integer.parseInt(prop.getProperty(INITIAL_STEP_NUMBER, "10")));
    	
    	SequentialSampler sesa = sg.make();
    	
    	sesa.run(numberOfThreads, source, initialSize, initialSteps);
    	
    	source.shutDown();
    	
    	try(Reader r1 = new FileReader(args[2]))
    	{
    		sesa.writeToFile(r1);
    	}
    }

    /**
     * 
     * @param prop
     * @param sg
     */
	public static void setUpGenerator(Properties prop, SamplerGenerator sg)
	{
		if(prop.containsKey(HISTORY_SIZE_PARAMETER))
    	{
    		sg.setHistorySize(Integer.parseInt(prop.getProperty(HISTORY_SIZE_PARAMETER)));
    	}
    	
    	if(prop.containsKey(REJUVENATION_STEPS_PARAMETER))
    	{
    		sg.setGibbsRounds(Integer.parseInt(prop.getProperty(REJUVENATION_STEPS_PARAMETER)));
    	}
    	
    	if(prop.containsKey(TOPIC_WORD_SMOOTHING))
    	{
    		sg.setTopicWordSmoothing(Double.parseDouble(prop.getProperty(TOPIC_WORD_SMOOTHING)));
    	}
    	
    	if(prop.containsKey(NUMBER_OF_TOPICS))
    	{
    		sg.setNumberOfTopics(Integer.parseInt(prop.getProperty(NUMBER_OF_TOPICS)));
    	}
    	
    	if(prop.containsKey(TOPIC_SMOOTHING))
    	{
    		sg.setTopicSmoothing(Double.parseDouble(TOPIC_SMOOTHING));
    	}
    	
    	if(prop.containsKey(SEED_PARAMETER))
    	{
    		sg.setSeed(Long.parseLong(prop.getProperty(SEED_PARAMETER)));
    	}
    	
    	if(prop.containsKey(PARTICLES_PARAMETER))
    	{
    		sg.setParticles(Integer.parseInt(prop.getProperty(PARTICLES_PARAMETER)));
    	}
	}
}
